package com.langhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.langhua.common.R;
import com.langhua.entity.OrderDetail;
import com.langhua.service.OrderDetailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单明细
 */
@Slf4j
@RestController
@RequestMapping("/orderDetails")
public class OrderDetailController {

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     * @param orderDetailList
     * @return
     */
    @PostMapping("/addList")
    public R<String> add(@RequestBody List<OrderDetail> orderDetailList) {
        log.info("订单明细表数据：{}", orderDetailList);
//        注：这个在shopping_car.html中查询当前用户购物车所有菜品之后将其给后端保存到订单明细表时候，id也会覆盖，所以也要是设置为空*/

//        for (int i = 0; i < orderDetailList.size(); i++) {
//            orderDetailList.get(i).setId(null);
//            orderDetailList.get(i).setOrderId(orderId);
//        }

//        LambdaQueryWrapper<OrderDetail> lqw =new LambdaQueryWrapper<>();
//        lqw.eq(OrderDetail::userId)

//        //随机生成一个订单号
//        //1.UUID生成32位数
//        String uuid = UUID.randomUUID().toString().replace("-", "");
//        //2.然后截取前面或后面16位
//        String substring = uuid.substring(0, 16);
//        orders.setOrderId(substring);
//        orders.setPayTime(LocalDateTime.now());
//
//        log.info("订单数据：{}", orders);


        orderDetailService.saveBatch(orderDetailList);
        return R.success("下单成功");
    }

    /**
     * 用户下单
     * @param orderDetail
     * @return
     */
    @PostMapping("/addOne")
    public R<String> addOne(@RequestBody OrderDetail orderDetail) {
        log.info("订单明细表数据：{}", orderDetail);

        orderDetailService.save(orderDetail);
        return R.success("下单成功");
    }


    /**
     * 订单明细表展示
     * @param current  当前页
     * @param size     每页条数
     * @param orderDetail   条件查询
     * @return
     */
    @GetMapping("{current}/{size}")
    public R<IPage> getPage(@PathVariable Integer current, @PathVariable Integer size, OrderDetail orderDetail){

        IPage page=new Page(current,size);
        LambdaQueryWrapper<OrderDetail> lqw=new LambdaQueryWrapper<OrderDetail>();
        lqw.like(Strings.isNotEmpty(orderDetail.getOrderId()),OrderDetail::getOrderId,orderDetail.getOrderId());

        IPage page1 = orderDetailService.page(page,lqw);
        System.out.println("订单明细表查询内容："+page1);
        return R.success(page1,"orderDetail");
    }

    /**
     * 通过订单id查询所有订单详情
     * @param orderId
     * @return
     */
    @GetMapping("getByOrderId/{orderId}")
    public R<List<OrderDetail>> getByOrderId(@PathVariable String orderId){

        LambdaQueryWrapper<OrderDetail> lqw =new LambdaQueryWrapper<>();
        lqw.eq(OrderDetail::getOrderId,orderId);
        List<OrderDetail> list = orderDetailService.list(lqw);
        return R.success(list,"通过订单id查询所有订单详情");
    }

    /**
     * 通过id删除订单明细表数据
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public R<String> deleteById(@PathVariable Long id){
        log.info("删除orderDetail的id为：{}", id);

        orderDetailService.removeById(id);
        return R.success(null,"删除成功！");
    }


    /**
     * 统计订单详情表的每个菜品的信息（数量，总价）
     * @return
     */
    @GetMapping("/getDIshTotal")
    public R<List<OrderDetail>> getDIshTotal(){

        List<OrderDetail> dIshTotal = orderDetailService.getDIshTotal();

        return R.success(dIshTotal,"统计订单详情表的每个菜品的信息（数量，总价）");
    }

}
